package com.rain.shoppingscienceproj.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.module.Goods;
import com.rain.shoppingscienceproj.utils.ImageUtils;
import com.rain.shoppingscienceproj.utils.toast.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Rain
 * Create: 2018/9/3 2:59
 */
public class SelectAgencyAdapter extends RecyclerView.Adapter<SelectAgencyAdapter.MyViewHolder> {

    private Context mContext;
    List<Goods> list;
    private CheckItemClickListener checkItemClickListener;

    public void setCheckItemClickListener(CheckItemClickListener checkItemClickListener) {
        this.checkItemClickListener = checkItemClickListener;
    }

    public void setList(List<Goods> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public SelectAgencyAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_shop_layout, parent, false);//解决宽度不能铺满
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Goods goods = list.get(position);
        if (null != goods.getGoodsImage() || !"".equals(goods.getGoodsImage())) {
            ImageUtils.image(mContext, goods.getGoodsImage(), holder.iv_image);
        }
        holder.tv_title.setText(goods.getGoodsName());
        holder.tv_money.setText(goods.getGoodsPay());
        holder.tv_goods_num.setText("库存:" + goods.getGoodsStock() + "          销售数量:" + goods.getGoodsNum());
        holder.et_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                goods.setGoodsLimit(Integer.parseInt(editable.toString()));
            }
        });

        holder.ll_check.setOnClickListener(view -> {
            if (!goods.isSelect()) {
                goods.setSelect(true);
                holder.iv_check.setImageResource(R.drawable.address_complete);
            } else {
                goods.setSelect(false);
                holder.iv_check.setImageResource(R.drawable.wallet_recharge_checkbox);
            }
            checkItemClickListener.checkItemClickListener(goods);
        });
    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.iv_check)
        ImageView iv_check;

        @BindView(R.id.ll_check)
        LinearLayout ll_check;

        @BindView(R.id.iv_image)
        ImageView iv_image;

        @BindView(R.id.tv_title)
        TextView tv_title;

        @BindView(R.id.tv_money)
        TextView tv_money;

        @BindView(R.id.tv_goods_num)
        TextView tv_goods_num;

        @BindView(R.id.tv_buy_num)
        TextView tv_buy_num;

        @BindView(R.id.et_num)
        EditText et_num;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface CheckItemClickListener {
        void checkItemClickListener(Goods goods);
    }
}
